﻿using System;
using System.Net;
using MyWeb.Business;
using MyWeb.Data;
using MyWeb.Common;

namespace MyWeb.Modules.Pages
{
    public partial class Contacts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                showContact();
            }
        }
        private void showContact()
        {
            var s = "<h1>Mọi chi tiết xin liên hệ</h1>\n";
            s += "<div class=\"contactview\">";
            var list = ConfigService.Config_GetById("1");
            if(list.Count >0)
            {
                LoadMetaConfig(list[0].Keyword, list[0].Description);
                s += list[0].Contact;
            }
            s += "</div>";
            ltrcontact.Text = s;
            #region html code
            /*
                <h1>Mọi chi tiết xin liên hệ</h1>
                <div class="contactview">
                    Coong ty coo đấ fdsf sada
                </div>
             */
            #endregion
        }
        #region Send email
        protected void SendMail()
        {
            //// Gmail Address from where you send the mail
            //var fromAddress = "Ở đây là mail gửi";
            ////Password of your gmail address
            //const string fromPassword = "Password mail gửi";
            //// any address where the email will be sending
            //var toAddress = "Ở đây nhập vào địa chỉ mail muốn nhận tin gửi tin về";

            var list = ConfigService.Config_GetById("1");

            
            var fromAddress = list[0].Mail_Noreply;

            string fromPassword = list[0].Mail_Password;
            
            var toAddress = list[0].Mail_Info;


            // Passing the values and make a email formate to display
            var subject = YourSubject.Text;
            var body = "From: " + YourName.Text + "\n";
            body += "Số điện thoại:" + YourPhone.Text + "\n";
            body += "Email: " + YourEmail.Text + "\n";
            body += "Subject: " + YourSubject.Text + "\n";
            body += "Nội dung: \n" + Comments.Text + "\n";
            // smtp settings
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = list[0].Mail_Smtp;
                smtp.Port = int.Parse(list[0].Mail_Port);
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            // Passing values to smtp object
            smtp.Send(fromAddress, toAddress, subject, body);
        }
        #endregion
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(TextBox1.Text);
            if (!ccJoin.UserValidated)
            {
                Label1.Text = "Mã bảo vệ chưa chính xác";
                return;
            }
            //here on button click what will done 
            var obj = new Contact();
            obj.Name = YourName.Text;
            obj.Company = "dichvuweb24h.com";
            obj.Mail = YourEmail.Text;
            obj.Phone = YourPhone.Text;
            obj.Adress = YourAdress.Text;
            obj.Subject = YourSubject.Text;
            obj.Detail = Comments.Text;
            obj.Date = DateTimeClass.ConvertDate(DateTime.Now.ToString());
            obj.Active = "1";
            obj.Lang = "vi";

            ContactService.Contact_Insert(obj);
            SendMail();

            ltrmsg.Text = "\tCảm ơn bạn đã sử dụng dịch vụ của chúng tôi.\n Chúng tôi sẽ liên lạc với bạn ngay sau khi nhận được thư";
            YourAdress.Text = "";
            YourEmail.Text = "";
            YourName.Text = "";
            Comments.Text = "";
            YourSubject.Text = "";
            YourPhone.Text = "";
            TextBox1.Text = "";
        }
        #region[LoadMetaConfig]
        void LoadMetaConfig(string key, string meta)
        {
            Page.Title = "Liên hệ, phản hồi";
            Page.MetaKeywords = key;
            Page.MetaDescription = meta;
        }
        #endregion
    }
}